<?php 
## include required files
/*******************************/
require_once("../includefiles.php");
require_once("../model/admindetails.php");
require_once ("../includes/classes/pagination_class.php");
require_once '../includes/functions.php'; 
require_once '../model/moduleuser.php';
require_once '../model/message.php'; 
require_once '../model/users.php'; 


/*******************************/
## Create Objects 
/*******************************/
$Obj =  new Model_Message();
$userObj =  new Model_Users();	
$moduleObj=new Model_ModuleUser();
/*******************************/

$msg_id = base64_decode($_GET['id']);
$page = $_GET['page'];
$smarty->assign('page', $page);

$msgArray = $Obj->getMessageDetailsById($msg_id);
$msgArray['from_name'] = $userObj->getUserNameByUserId($msgArray['from_id']);
$msgArray['to_name'] = $userObj->getUserNameByUserId($msgArray['to_id']);
$smarty->assign("msgArray",$msgArray);

if(isset($_POST['Send']) && $_POST['Send']=='Send')
{
	$msgAddArray = array();
	if($msgArray['to_id'] != $_SESSION['admin_id'])
	{
		$msgAddArray['from_id']   = 1; 
		$msgAddArray['to_id'] 	  =$msgArray['to_id'];
	}
	else
	{
		$msgAddArray['from_id']   = 1; 
		$msgAddArray['to_id'] 	  =$msgArray['from_id'];
	}
	$msgAddArray['subject']   = return_post_value($_POST['subject']);
	$msgAddArray['message']   = return_post_value($_POST['message'])."<br> ".$_POST['sign'];
	$msgAddArray['send_date'] = getCurDate();
	$msgAddArray['label'] 	  = 'inbox';
	$Obj->addMessage($msgAddArray); # add record for inbox
	
	$msgAddArray['label'] 	  = 'outbox';
	$msgObj->addMessage($msgAddArray); # add record for outbox

	$_SESSION['msg']="Message sent successfully"; 
	header("location:".SITE_URL."/admin/home.php?q=detail_msg&id=".base64_encode($msg_id));	
}



## Set active class variable for left menu  (main menu and sub menu)
$smarty->assign('activeclass', 'messages');
$smarty->assign('mainmenu', '3');

## Unset all the objects created which are on this page
unset($Obj);
unset($moduleObj);

$smarty->display(TEMPLATEDIR_ADMIN. 'messages/detail_msg.tpl');
unset($smarty);
?>